﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Liang.Util.Crypt
{
    /// <summary>
    /// 对称加解密
    /// </summary>
    public class DESCrypt : ICrypt
    {
        public bool Encry(ref byte[] buf, string strKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);

                using (MemoryStream ms = new MemoryStream())
                {
                    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(buf, 0, buf.Length);
                    cs.FlushFinalBlock();

                    buf = ms.ToArray();
                }

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Decry(ref byte[] buf, string strKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);

                using (MemoryStream ms = new MemoryStream())
                {
                    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                    cs.Write(buf, 0, buf.Length);
                    cs.FlushFinalBlock();

                    buf = ms.ToArray();
                }

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strData"></param>
        /// <param name="strKey">长度固定为8位</param>
        /// <returns></returns>
        public string EncStr(string strData, string strKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(strData);

                Encry(ref inputByteArray, strKey);

                StringBuilder sb = new StringBuilder();
                foreach (byte b in inputByteArray.ToArray())
                {
                    sb.AppendFormat("{0:x2}", b);
                }

                return sb.ToString();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strData"></param>
        /// <param name="strKey">长度固定为8位</param>
        /// <returns></returns>
        public string DecStr(string strData, string strKey)
        {
            try
            {
                byte[] inputByteArray = new byte[strData.Length / 2];
                for (int x = 0; x < strData.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(strData.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                Decry(ref inputByteArray, strKey);

                return System.Text.Encoding.Default.GetString(inputByteArray.ToArray());
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// MD5
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        //public string MdigestMD5(string source)
        //{
        //    MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
        //    byte[] output = MD5CSP.ComputeHash(Encoding.UTF8.GetBytes(source));
        //    return Convert.ToBase64String(output);
        //}
    }
}
